package demo.imagegirl;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

/**
 * 演示现在市场上的一些搓美女衣服的原理程序
 */
public class MainActivity extends Activity {

	ImageView iv_after, iv_pre;
	private Bitmap alterBitMap;
	private Canvas canvas;
	private Paint paint;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		BitmapFactory.Options options = new Options();
		options.inSampleSize = 2;
		iv_after = (ImageView) findViewById(R.id.iv_after);
		iv_pre = (ImageView) findViewById(R.id.iv_pre);

		Bitmap after = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher, options);
		Bitmap pre = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher, options);
		//可以修改的空白Bitmap
		alterBitMap = Bitmap.createBitmap(pre.getWidth(),pre.getHeight(),pre.getConfig());
		canvas = new Canvas(alterBitMap);
		paint  = new Paint();
		paint.setStrokeWidth(5);
		paint.setColor(Color.BLACK);
		canvas.drawBitmap(pre, new Matrix(), paint);

		iv_after.setImageBitmap(after);
		iv_pre.setImageBitmap(alterBitMap);

		iv_pre.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction() & MotionEvent.ACTION_MASK) {

				case MotionEvent.ACTION_MOVE:
					int x = (int) event.getX();
					int y  = (int) event.getY();
					for(int i=-8;i<8;i++){
						for(int j=-8;j<8;j++){
							alterBitMap.setPixel(x+i, y+j, Color.TRANSPARENT);
						}
					}
					iv_pre.setImageBitmap(alterBitMap);

					break;

				}
				return true;
			}
		});

	}

}
